package com.wjw.service;

import com.wjw.mapper.PavementFlatnessTableMapper;
import com.wjw.model.Pavement_flatness_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Facility_damage_table_view;
import com.wjw.viewmodel.Pavement_flatness_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PavementFlatnessTableService {


//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private PavementFlatnessTableMapper mapper=sqlsession.getMapper(PavementFlatnessTableMapper.class);


    public List<Pavement_flatness_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementFlatnessTableMapper mapper=sqlsession.getMapper(PavementFlatnessTableMapper.class);
        try {
            List<Pavement_flatness_table> all = mapper.findAll();
            List<Pavement_flatness_table_view> list= new ArrayList<Pavement_flatness_table_view>();
            for(Pavement_flatness_table e:all){
                Pavement_flatness_table_view k = new Pavement_flatness_table_view();
                k.setEmployee_testing_name(mapper.findEmployeeById(e.getEmployee_testing_id()).getEmployee_name());
                k.setPavement_flatness_date(e.getPavement_flatness_date());
                k.setPavement_flatness_id(e.getPavement_flatness_id());
                k.setPavement_flatness_IRI(e.getPavement_flatness_IRI());
                k.setPavement_flatness_remarks(e.getPavement_flatness_remarks());
                k.setRoad_basic_id(e.getRoad_basic_id());
                k.setRoad_basic_name2(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Pavement_flatness_table_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementFlatnessTableMapper mapper=sqlsession.getMapper(PavementFlatnessTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Pavement_flatness_table> all = mapper.findByPage(start,limit);
            List<Pavement_flatness_table_view> list= new ArrayList<Pavement_flatness_table_view>();
            for(Pavement_flatness_table e:all){
                Pavement_flatness_table_view k = new Pavement_flatness_table_view();
                k.setEmployee_testing_name(mapper.findEmployeeById(e.getEmployee_testing_id()).getEmployee_name());
                k.setPavement_flatness_date(e.getPavement_flatness_date());
                k.setPavement_flatness_id(e.getPavement_flatness_id());
                k.setPavement_flatness_IRI(e.getPavement_flatness_IRI());
                k.setPavement_flatness_remarks(e.getPavement_flatness_remarks());
                k.setRoad_basic_id(e.getRoad_basic_id());
                k.setRoad_basic_name2(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Pavement_flatness_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementFlatnessTableMapper mapper=sqlsession.getMapper(PavementFlatnessTableMapper.class);
        try {
            Pavement_flatness_table k = new Pavement_flatness_table();
            k.setEmployee_testing_id(mapper.findEmployeeByName(e.getEmployee_testing_name()).getEmployee_id());
            k.setPavement_flatness_date(e.getPavement_flatness_date());
            k.setPavement_flatness_id(e.getPavement_flatness_id());
            k.setPavement_flatness_IRI(e.getPavement_flatness_IRI());
            k.setPavement_flatness_remarks(e.getPavement_flatness_remarks());
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name2()).getRoad_basic_id());
            k.setRoad_basic_name2(e.getRoad_basic_name2());
            mapper.add(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementFlatnessTableMapper mapper=sqlsession.getMapper(PavementFlatnessTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean change(Pavement_flatness_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        PavementFlatnessTableMapper mapper=sqlsession.getMapper(PavementFlatnessTableMapper.class);
        try {
            Pavement_flatness_table k = new Pavement_flatness_table();
            k.setEmployee_testing_id(mapper.findEmployeeByName(e.getEmployee_testing_name()).getEmployee_id());
            k.setPavement_flatness_date(e.getPavement_flatness_date());
            k.setPavement_flatness_id(e.getPavement_flatness_id());
            k.setPavement_flatness_IRI(e.getPavement_flatness_IRI());
            k.setPavement_flatness_remarks(e.getPavement_flatness_remarks());
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name2()).getRoad_basic_id());
            k.setRoad_basic_name2(e.getRoad_basic_name2());
            mapper.change(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }
}
